#!/usr/bin/env python
# -*- coding: utf-8 -*- #   
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import cgi
from google.appengine.api import users
import Cookie,urllib
from django.utils import simplejson
from google.appengine.api import urlfetch

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp.util import run_wsgi_app
import time
from google.appengine.ext import db
import SiteType

class ShowBlock(webapp.RequestHandler):
	def get(self):
		user=SiteType.Header(self)
		admined=SiteType.Admined(self)
		url=self.request.uri
		thekey=SiteType.getkey(url,'block/')
		if thekey.find('/')==-1:
			theblock=db.get(thekey)
		self.response.out.write(u"""<div align="center">
		<img src=%s  border="0" alt=""><br>"""%SiteType.SiteLogo)
		self.response.out.write(u"""
		<font size="+5" color="#FFA500"> 欢迎来到<br> %s 论坛</font><br>"""%SiteType.SiteName)
		self.response.out.write(u"""<div align="left">""")
		if thekey.find('/')==-1:
			if theblock.belongto:
				if theblock.belongto!="" and thekey.find('/')==-1:
					self.response.out.write(u"""<a href="/forum/block/%s">返回上层</a>"""%theblock.belongto)
				else:
					self.response.out.write(u"""<a href="/forum">返回上层</a>""")
			else:
				self.response.out.write(u"""<a href="/forum">返回上层</a>""")
		if admined:
			self.response.out.write(u"""&nbsp;&nbsp;&nbsp;&nbsp;<a href="/forum/addblock?block=%s">添加板块</a>"""%theblock.key())
		self.response.out.write(u"""&nbsp;&nbsp;&nbsp;&nbsp;<a href="/art/edit?block=%s">发帖</a></div><font size="+2" color="#FFA500">"""%theblock.key()+theblock.name+"</font>")
		Level1Blocks=db.GqlQuery("SELECT * FROM Block WHERE belongto='%s'"%theblock.key())
		if Level1Blocks.count()==0:
			pass
		else:
			self.response.out.write(u"""<fieldset><legend>板块</legend>""")
			for block in Level1Blocks:
				self.response.out.write("""<fieldset>""")
				self.response.out.write("""<a href="/forum/block/%s">"""%block.key()+"""%s</a>"""%block.name+"""<br>%s"""%block.introduction)
				if admined:
					self.response.out.write(u"""<div align="right"><a href="/forum/delblock?block=%s">删除此版</a></div>"""%block.key())
				self.response.out.write("""</fieldset>""")
			self.response.out.write(u"</fieldset>")
		arts=db.GqlQuery(u"SELECT * FROM Article WHERE belongto='%s'"%theblock.key())
		self.response.out.write(u"""<fieldset><legend>帖子</legend>""")
		if arts.count()==0:
			self.response.out.write(u"""暂时还没有帖子""")
		else:
			for art in arts:
				self.response.out.write("""<fieldset>""")
				self.response.out.write(u"""%s:&nbsp;&nbsp;&nbsp;&nbsp</a>"""%art.author.nickname()+"""<a href="/art/show/%s"""%art.key()+u"""?block=%s">"""%theblock.key()+u"""%s</a>"""%art.title+u"""&nbsp;&nbsp;&nbsp;&nbsp;%s回复&nbsp;&nbsp;&nbsp;&nbsp;"""%art.num_reply+"""%s"""%art.date)
				if admined:
					self.response.out.write(u"""<div align="right"><a href="/art/delart?art=%s">删除此文</a></div>"""%art.key())
					self.response.out.write(u"""<div align="right"><a href="/art/createnews?art=%s">顶为新闻</a></div>"""%art.key())
				self.response.out.write("""</fieldset>""")
		self.response.out.write(u"</fieldset>")
		SiteType.Ender(self)
class AddBlock(webapp.RequestHandler):
	def get(self):
		user=SiteType.Header(self)
		self.response.out.write(u"""<div align="center"><fieldset>""")
		self.response.out.write(u"""<form action="/forum/add" method="post">
				<div>版块名：<input type="text" name="name"></div>
				<div>　简介：<textarea name="intro" rows="3" cols="60"></textarea></div>
				<div><input type="submit" value="添加"></div>
			</form></fieldset>""")
		self.response.out.write(u"</fieldset>")
		SiteType.Ender(self)
class DEL(webapp.RequestHandler):
	def get(self):
		user=users.get_current_user()
		thekey=self.request.url
		thekey=SiteType.getkey(thekey,'block=')	
		if user:
			if SiteType.isadmin(user):
				db.delete(thekey)
		self.redirect(self.request.headers['referer'])
class ADD(webapp.RequestHandler):
	def post(self):
		user=users.get_current_user()
		thekey=self.request.headers['referer']
		thekey=SiteType.getkey(thekey,'block=')	

		if user:
			if SiteType.isadmin(user):
				block=SiteType.Block()
				block.name=self.request.get('name')
				block.introduction=self.request.get('intro')
				block.belongto=thekey
				if thekey.find('/add')==-1:
					block.level=db.get(thekey).level+1
					
				else:
					block.level=0
					block.belongto=""
				block.put()
		if thekey.find('/add')==-1:
			self.redirect("/forum/block/%s"%thekey)
		else:
			self.redirect("/forum")
class forum(webapp.RequestHandler):
	def get(self):
		user=SiteType.Header(self)
		admined=SiteType.Admined(self)
		self.response.out.write(u"""<div align="center">
		<img src=%s  border="0" alt=""><br>"""%SiteType.SiteLogo)
		self.response.out.write(u"""
		<font size="+5" color="#FFA500"> 欢迎来到<br> %s 论坛</font><br>"""%SiteType.SiteName)
		if admined:
			self.response.out.write(u"""<div align="left"><a href="/forum/addblock">添加板块</a></div>""")
		self.response.out.write(u"""<fieldset><legend>论坛</legend>""")
		Level1Blocks=db.GqlQuery("SELECT * FROM Block WHERE level=0")
		if Level1Blocks.count()==0:
			self.response.out.write(u"暂时没有板块")
		else:
			for block in Level1Blocks:
				self.response.out.write("<fieldset>")
				self.response.out.write(u"""<a href="/forum/block/%s">"""%block.key()+"""%s</a>"""%block.name+"""<br>%s"""%block.introduction)
				if admined:
					self.response.out.write(u"""<div align="right"><a href="/forum/delblock?block=%s">删除此版</a></div>"""%block.key())
				self.response.out.write("</fieldset>")
		self.response.out.write(u"</fieldset>")
		SiteType.Ender(self)

def main():
  application = webapp.WSGIApplication([('/forum', forum),('/forum/block/.*',ShowBlock),('/forum/addblock.*',AddBlock),('/forum/add',ADD),('/forum/delblock.*',DEL)],
                                       debug=True)
  util.run_wsgi_app(application)

if __name__ == '__main__':
  main()
